<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <div th:replace="header::common_resource"></div>
</head>
<body>
    <div class="demoTable">
        名字：
        <div class="layui-inline">
            <input class="layui-input" name="name" id="nameId" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload">搜索</button>
    </div>

    <table class="layui-hide" id="test" lay-filter="test"></table>

    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="deleteAll">批量删除</button>
            <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        </div>
    </script>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/html" id="statusTemplet">
        <!-- 这里的 checked 的状态只是演示 -->
        <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="激活|禁用" lay-filter="statusLayFilter" {{ d.status == 1 ? 'checked' : '' }}>
    </script>

    <script type="text/html" id="imageTemplet">
        <img src="{{d.mainImage}}"/>
    </script>

    <script>
        layui.use('table', function(){
            var table = layui.table;
            var form = layui.form;

            table.render({
                elem: '#test'
                ,url:'/product/selectByPage'
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    ,layEvent: 'LAYTABLE_TIPS'
                    ,icon: 'layui-icon-tips'
                }]
                ,title: '用户数据表'
                ,cols: [
                    [
                        {type: 'checkbox', fixed: 'left'}
                        ,{field:'id', title:'ID', fixed: 'left'}
                        ,{field:'name', title:'商品名'}
                        ,{field:'subtitle', title:'子标题'}
                        ,{field:'categoryId', title:'分类'}
                        ,{field:'mainImage', title:'封面',templet: '#imageTemplet'}
                        ,{field:'detail', title:'商品内容',escape:false}
                        ,{field:'price', title:'价格'}
                        ,{field:'stock', title:'库存'}
                        ,{field:'status', title:'状态',templet: '#statusTemplet'}
                        ,{field:'gmtCreate', title:'创建时间'}
                        ,{field:'gmtModified', title:'修改时间'}
                        ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
                    ]
                ]
                ,page: true
                ,id: 'tableId'
            });

            //监听性别操作
            form.on('switch(statusLayFilter)', function(obj){
                layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
                var status = obj.elem.checked == true ? 1 : 0;
                $.post(
                    '/blog?method=updateStatus',
                    {'id':this.value, 'status': status},
                    function(result) {
                        if (result.code == 0) {
                            mylayer.okMsg(result.msg);
                        }
                    },
                    'json'
                );
            });

            //头工具栏事件
            table.on('toolbar(test)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'deleteAll':
                        var data = checkStatus.data;
                        var ids = new Array();
                        $(data).each(function() {
                            ids.push(this.id)
                        });
                        //[2,4]
                        ids = ids.join(',');// '2,4'
                        layer.confirm('真的删除行么', function(index){
                            $.post(
                                '/product/deleteAll',
                                {'ids': ids},
                                function(result) {
                                    if (result.code == 0) {
                                        mylayer.okMsg(result.msg);
                                        //删除之后重新刷新这个表格
                                        table.reload('tableId');
                                    }
                                },
                                'json'
                            );
                        });
                        break;
                    case 'add':
                        layer.open({
                            type: 2,
                            area: ['850px', '650px'],
                            content: '/product/getProductAddPage'
                        });
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选': '未全选');
                        break;

                    //自定义头工具栏右侧图标 - 提示
                    case 'LAYTABLE_TIPS':
                        layer.alert('这是工具栏右侧自定义的一个图标按钮');
                        break;
                };
            });

            //监听行工具事件
            table.on('tool(test)', function(obj){
                //{id: 2, name: 'zhansgan', password: '23', age: 0, gender: null}
                var data = obj.data;
                console.log(obj)
                if(obj.event === 'del'){
                    layer.confirm('真的删除行么', function(index){
                        $.post(
                            '/product/deleteById',
                            {'id': data.id},
                            function(result) {
                                if (result.code == 0) {
                                    mylayer.okMsg(result.msg);
                                    //删除之后重新刷新这个表格
                                    table.reload('tableId');
                                }
                            },
                            'json'
                        );
                    });
                } else if(obj.event === 'edit'){
                    layer.open({
                        type: 2,
                        area: ['550px', '350px'],
                        content: '/blog?method=getBlogUpdatePage&id=' + data.id
                    });
                }
            });

            var $ = layui.$, active = {
                reload: function(){
                    //执行重载
                    table.reload('tableId', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            name: $('#nameId').val()
                        }
                    });
                }
            };

            $('.demoTable .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });


        });
    </script>
</body>
</html>